using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using BONSAI.Common;
using BONSAI.Common.DTO;

namespace BONSAI.DAL
{
	public class ShoppingCartDAO
	{
		public const string SHOPPINGCART_GET_BY_ID = "ShoppingCartGetByID";
		public const string SHOPPINGCART_GET_ALL = "ShoppingCartGetAll";
		public const string SHOPPINGCART_INSERT = "ShoppingCartInsert";
		public const string SHOPPINGCART_UPDATE_BY_ID = "ShoppingCartUpdateByID";
		public const string SHOPPINGCART_DELETE_BY_ID = "ShoppingCartDeleteByID";

		public const string SHOPPINGCARTID = "ShoppingCartID";
		public const string ORDERNUMBER = "OrderNumber";
		public const string FULLNAME = "FullName";
		public const string ADDRESS = "Address";
		public const string PHONE = "Phone";
		public const string CONTENT = "Content";
		public const string PRODUCTSID = "ProductsID";
		public const string CREATEDATE = "Createdate";

		#region region properties
		public Database Database { get; set; }
		#endregion

		#region Constructor
		public ShoppingCartDAO()
		{
			Database = DatabaseFactory.CreateDatabase(BONSAIConstants.BONSAI_CONNECTION_STRING);
		}
		#endregion


		public ShoppingCartDTO GetByID(string id)
		{
			var command = Database.GetStoredProcCommand(SHOPPINGCART_GET_BY_ID);
			try
			{
				Database.AddInParameter(command, SHOPPINGCARTID, DbType.String, id);
				var dataReader = Database.ExecuteReader(command);
				while (dataReader.Read())
				{
					var item = GetShoppingCartFromDataReader(dataReader);
					return item;
				}
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public IList<ShoppingCartDTO> GetAll()
		{
			var command = Database.GetStoredProcCommand(SHOPPINGCART_GET_ALL);
			try
			{
				var dataReader = Database.ExecuteReader(command);
				var lstItem = new List<ShoppingCartDTO>();
				while (dataReader.Read())
				{
					var item = GetShoppingCartFromDataReader(dataReader);
					lstItem.Add(item);
				}
				return lstItem;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public string Insert(ShoppingCartDTO shoppingcart)
		{
			var command = Database.GetStoredProcCommand(SHOPPINGCART_INSERT);
			try
			{
				Database.AddOutParameter(command, SHOPPINGCARTID, DbType.String, 36);
				Database.AddInParameter(command, ORDERNUMBER, DbType.String, shoppingcart.OrderNumber);
				Database.AddInParameter(command, FULLNAME, DbType.String, shoppingcart.FullName);
				Database.AddInParameter(command, ADDRESS, DbType.String, shoppingcart.Address);
				Database.AddInParameter(command, PHONE, DbType.String, shoppingcart.Phone);
				Database.AddInParameter(command, CONTENT, DbType.String, shoppingcart.Content);
				Database.AddInParameter(command, PRODUCTSID, DbType.String, shoppingcart.ProductsID);
				Database.AddInParameter(command, CREATEDATE, DbType.String, shoppingcart.Createdate);
				var dataReader = Database.ExecuteReader(command);
				return Database.GetParameterValue(command, SHOPPINGCARTID).ToString();
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public void UpdateByID(ShoppingCartDTO shoppingcart)
		{
			var command = Database.GetStoredProcCommand(SHOPPINGCART_UPDATE_BY_ID);
			try
			{
				Database.AddInParameter(command, SHOPPINGCARTID, DbType.String, shoppingcart.ShoppingCartID);
				Database.AddInParameter(command, ORDERNUMBER, DbType.String, shoppingcart.OrderNumber);
				Database.AddInParameter(command, FULLNAME, DbType.String, shoppingcart.FullName);
				Database.AddInParameter(command, ADDRESS, DbType.String, shoppingcart.Address);
				Database.AddInParameter(command, PHONE, DbType.String, shoppingcart.Phone);
				Database.AddInParameter(command, CONTENT, DbType.String, shoppingcart.Content);
				Database.AddInParameter(command, PRODUCTSID, DbType.String, shoppingcart.ProductsID);
				Database.AddInParameter(command, CREATEDATE, DbType.String, shoppingcart.Createdate);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		public void DeleteByID(string shoppingcartid)
		{
			var command = Database.GetStoredProcCommand(SHOPPINGCART_DELETE_BY_ID);
			try
			{
				Database.AddInParameter(command, SHOPPINGCARTID, DbType.String, shoppingcartid);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		private static ShoppingCartDTO GetShoppingCartFromDataReader(IDataRecord dataReader)
		{
			var item = new ShoppingCartDTO();
			try
			{

				item.ShoppingCartID = (string.IsNullOrEmpty(Convert.ToString(dataReader[SHOPPINGCARTID]))) ? "" : Convert.ToString(dataReader[SHOPPINGCARTID]);
				item.OrderNumber = (string.IsNullOrEmpty(Convert.ToString(dataReader[ORDERNUMBER]))) ? 0 : Convert.ToInt32(dataReader[ORDERNUMBER].ToString());
				item.FullName = (string.IsNullOrEmpty(Convert.ToString(dataReader[FULLNAME]))) ? "" : Convert.ToString(dataReader[FULLNAME]);
				item.Address = (string.IsNullOrEmpty(Convert.ToString(dataReader[ADDRESS]))) ? "" : Convert.ToString(dataReader[ADDRESS]);
				item.Phone = (string.IsNullOrEmpty(Convert.ToString(dataReader[PHONE]))) ? "" : Convert.ToString(dataReader[PHONE]);
				item.Content = (string.IsNullOrEmpty(Convert.ToString(dataReader[CONTENT]))) ? "" : Convert.ToString(dataReader[CONTENT]);
				item.ProductsID = (string.IsNullOrEmpty(Convert.ToString(dataReader[PRODUCTSID]))) ? "" : Convert.ToString(dataReader[PRODUCTSID]);
				item.Createdate = (string.IsNullOrEmpty(Convert.ToString(dataReader[CREATEDATE]))) ? DateTime.MinValue : DateTime.Parse(dataReader[CREATEDATE].ToString());
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return item;
		}
	}
}

